package com.demo.userprofile.component.utils;

import java.util.Map;

import org.mvel2.MVEL;

import com.google.common.collect.Maps;
import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;

/**
 * 规则表达式引擎代码示例
 *
 * @author userprofile_demo
 */
public class MvelUtils {

    public static void main(String[] args) {
        long userId = 100l;
        String expression = "province == '北京市' && gender == '男'";
        Map<String, Object> map = Maps.newHashMap();
        // map.put("province", getLabelValue(userId, "province"));
        // map.put("gender", getLabelValue(userId, "gender"));
        map.put("province", "北京市");
        map.put("gender", "男");

        // 通过MVEL实现
        Boolean mvelResult = (Boolean) MVEL.eval(expression, map);
        if (mvelResult) {
            // 规则判存结果是"是"
            System.out.println("是");
        } else {
            // 规则判存结果是"否"
            System.out.println("否");
        }

        // 通过Aviator实现
        Expression compiledExp = AviatorEvaluator.compile(expression);
        Boolean aviatorResult = (Boolean) compiledExp.execute(map);
        if (aviatorResult) {
            // 规则判存结果是"是"
            System.out.println("是");
        } else {
            // 规则判存结果是"否"
            System.out.println("否");
        }
    }
}


